<?php
namespace Home\Controller;
use Think\Controller;

class AjaxWorkerController extends Controller{
	//Ajax分流
	function doAjax(){
		$this->begin();
		$fun_type = $_GET['fun_type'];
		$ajaxMsg = "No message!";
		switch($fun_type){
			case "viewInfo":$ajaxMsg = $this->viewInfo(); break;
			case "editInfo":
				$name = strip_tags($_REQUEST['name']);
				$sex = strip_tags($_REQUEST['sex']);
				$birthday = strip_tags($_REQUEST['birthday']);
				$email = strip_tags($_REQUEST['email']);
				$branch = strip_tags($_REQUEST['branch']);
				$phonenum = strip_tags($_REQUEST['phonenum']);
				$banknum = strip_tags($_REQUEST['banknum']);
				$bank = strip_tags($_REQUEST['bank']);
				$entryday = strip_tags($_REQUEST['entryday']);
				$leaveday = strip_tags($_REQUEST['leaveday']);
				$persionid = strip_tags($_REQUEST['persionid']);
				
				$ajaxMsg = $this->editInfo($name, $sex, $birthday, $email, $branch, $phonenum, $banknum, $bank, $entryday, $leaveday, $persionid);
			break;
			case "viewFile":$ajaxMsg = $this->viewFile();break;
			case "deleteFile":$ajaxMsg = $this->deleteFile($_GET['filename']); break;
			case "adminPage":	$ajaxMsg = $this->adminPage();	break;
			case "adminEdit":
				$workerid	=	strip_tags($_GET['workerid']);
				$name		=	strip_tags($_GET['name']);
				$birthday	=	strip_tags($_GET['birthday']);
				$email		=	strip_tags($_GET['email']);
				$branch		=	strip_tags($_GET['branch']);
				$phonenum	=	strip_tags($_GET['phonenum']);
				$banknum	=	strip_tags($_GET['banknum']);
				$bank		=	strip_tags($_GET['bank']);
				$entryday	=	strip_tags($_GET['entryday']);
				$leaveday	=	strip_tags($_GET['leaveday']);
				$persionid	=	strip_tags($_GET['persionid']);
				
				$data = array($workerid,$name,$birthday,$email,$branch,$phonenum,$banknum,$bank,$entryday,$leaveday,$persionid);
				$ajaxMsg = $this->adminEdit($data);
			break;
			
			case "adminFile":	
				$workerid = strip_tags($_GET['workerid']);
				$ajaxMsg = $this->adminFile($workerid);	
			break;
		}
		$this->ajaxReturn($ajaxMsg);
	}
	//初始化个人信息
	function begin(){
		$worker = new \Home\Common\Worker();
		$ip = get_client_ip();
		$workerInfo = $worker->selectItem("iry_worker_info",array("workerip"=>"'$ip'"),null);
		cookie("iry_worker_id",$worker_info[0]['workerid'],C('cookie_time'));
		cookie("iry_worker_name",$worker_info[0]['name'],C('cookie_time'));
		cookie("iry_worker_sex",$worker_info[0]['sex'],C('cookie_time'));
		cookie("iry_worker_birthday",$worker_info[0]['birthday'],C('cookie_time'));
		cookie("iry_worker_faceimg",$worker_info[0]['faceimg'],C('cookie_time'));
		cookie("iry_worker_picture",$worker_info[0]['picture'],C('cookie_time'));
		cookie("iry_worker_phonenum",$worker_info[0]['phonenum'],C('cookie_time'));
		cookie("iry_worker_email",$worker_info[0]['email'],C('cookie_time'));
		cookie("iry_worker_branch",$worker_info[0]['branch'],C('cookie_time'));
		cookie("iry_worker_banknum",$worker_info[0]['banknum'],C('cookie_time'));
		cookie("iry_worker_bank",$worker_info[0]['bank'],C('cookie_time'));
		cookie("iry_worker_persionid",$worker_info[0]['persionid'],C('cookie_time'));
		cookie("iry_worker_entryday",$worker_info[0]['entryday'],C('cookie_time'));
		cookie("iry_worker_leaveday",$worker_info[0]['leaveday'],C('cookie_time'));
		
		session('iry_worker_id',		$workerInfo[0]['workerid']);
		session('iry_worker_name',		$workerInfo[0]['name']);
		session('iry_worker_sex',		$workerInfo[0]['sex']);
		session('iry_worker_birthday',	$workerInfo[0]['birthday']);
		session('iry_worker_faceimg',	$workerInfo[0]['faceimg']);
		session('iry_worker_phonenum',	$workerInfo[0]['phonenum']);
		session('iry_worker_email',		$workerInfo[0]['email']);
		session('iry_worker_branch',	$workerInfo[0]['branch']);
		session('iry_worker_banknum',	$workerInfo[0]['banknum']);
		session('iry_worker_bank',		$workerInfo[0]['bank']);
		session('iry_worker_entryday',	$workerInfo[0]['entryday']);
		session('iry_worker_leaveday',	$workerInfo[0]['leaveday']);
		session('iry_worker_persionid',	$workerInfo[0]['persionid']);
	}
	//admin文件预览
	function adminFile($workerid){
		$worker = new \Home\Common\Worker();
		$arFile = $worker->selectItem("iry_worker_file",array("workerid"=>$workerid,"exist"=>1),null,null);
		
		$table = "<table width='500' cellspacing='10'><tr><th colspan='3'>员工附件查看</th></tr>";
		$nums = 0;
		foreach($arFile as $aFile){
			$nums++;
			$table .= "<tr><td>".$nums.".</td><td>".$aFile['type']."</td><td><a href='../Index/download?filename=".$aFile['filesrc']."'>下载</a></td></tr>";
		}
		$table .= "<tr><td colspan='3'><button class='button-2' onclick='disAdminFile()'>取消</td></tr></table>";
		
		return $table;
	}
	//后台信息更改
	function adminEdit($data){
		$worker =new \Home\Common\Worker();
		$set_ = array(
				"name"		=>	"'$data[1]'",
				"birthday"	=>	"'$data[2]'",
				"email"		=>	"'$data[3]'",
				"branch"	=>	$data[4],
				"phonenum"	=>	"'$data[5]'",
				"banknum"	=>	"'$data[6]'",
				"bank"		=>	"'$data[7]'",
				"entryday"	=>	"'$data[8]'",
				"leaveday"	=>	"'$data[9]'",
				"persionid"	=>	"'$data[10]'"
		);
		$where_ = array("workerid"	=>	$data[0]);
		$status = $worker->updateItem("iry_worker_info", $set_, $where_);
		
		return $status?"success":"error";
	}
	//admin信息获取
	function adminPage(){
		//权限认证
		$atoy = ipAtoy(get_client_ip());
		if($atoy){
			$worker = new \Home\Common\Worker();
			$arWroker = $worker->selectItem("iry_worker_info",null,null,null);
		
			$table = "<table cellpadding='0' cellspacing='0' id='example' class='info-table'>";
			$table .= "<tr><th>姓名</th><th>性别</th><th>生日</th><th>手机号</th><th>电子邮件</th><th>部门</th><th>入职日期</th><th>详情信息</th><th>附件查看</th></tr>";
			foreach($arWroker as $aWorker){
				$aWorker['sex'] = C("sex_name.".$aWorker['sex']);
				$workerInfo = $aWorker['workerid'].":".$aWorker['name'].":".$aWorker['sex'].":".$aWorker['birthday'].":".$aWorker['phonenum'].":".
						$aWorker['email'].":".$aWorker['branch'].":".$aWorker['entryday'].":".$aWorker['leaveday'].":".$aWorker['persionid'].":".$aWorker['banknum'].
				":".$aWorker['bank'];
				$table .= "<tr><td>".$aWorker['name']."</td><td>".$aWorker['sex']."</td><td>".$aWorker['birthday']."</td><td>".$aWorker['phonenum']."</td>
						<td>".$aWorker['email']."</td><td>".C("branch_name.".$aWorker['branch'])."</td><td>".$aWorker['entryday']."</td><td>
						<button onclick=adminDetail('".$workerInfo."')>详情</button></td><td><button onclick=adminFile('".$aWorker['workerid']."')>附件</button></td><tr>";
			}
			$table .= "</table>";
			
			return $table;
			
		}else{
			return "error";
		}
	}
	//删除职员上传附件
	function deleteFile($filename){
		$worker = new \Home\Common\Worker();
		$status = $worker->updateItem("iry_worker_file", array("exist"=>0), array("filesrc"=>"'$filename'"));
		return $status?"success":"error";
	}
	//显示职员附件
	function viewFile(){
		$worker = new \Home\Common\Worker();
		$workerid = session("iry_worker_id");
		$arFile = $worker->selectItem("iry_worker_file",array("workerid"=>$workerid,"exist"=>1));
		
		$table = "<table cellpadding='0' cellspacing='0' width='500'>
					<tr><th colspan='3'>个人附件列表</th></tr>";
		foreach($arFile as $file){
			$table .= "<tr><td width='100'>".$file['type']."</td><td><a href='../Index/downLoad?filename=".$file['filesrc']."'>下载</a>
					<a onClick=deleteSelfFile('".$file['filesrc']."')>删除</a></td>
					<td></td></tr>";
		}
		$table .= "<tr><td></td><td colspan='2'><input type='button' value='上传附件' onclick='enSelfFilePanel()'></td></tr>";
		$table .= "</table>";
		
		return $table;
	}
	//编辑个人信息
	function editInfo($name,$sex,$birthday,$email,$branch,$phonenum,$banknum,$bank,$entryday,$leaveday,$persionid){
		$worker = new \Home\Common\Worker();
		$workerip = get_client_ip();
		if(session("iry_worker_id") != null){
			$set_ = array(
				"name"	=>	"'$name'",
				"sex"	=>	$sex,
				"birthday"	=>	"'$birthday'",
				"email"	=>	"'$email'",
				"branch"	=>	$branch,
				"phonenum"	=>	"'$phonenum'",
				"banknum"	=>	"'$banknum'",
				"bank"		=>	"'$bank'",
				"entryday"	=>	"'$entryday'",
				"leaveday"	=>	"'$leaveday'",
				"persionid"	=>	"'$persionid'",
				"workerip"	=>	"'$workerip'"
			);
			$where_ = array("workerid"=>session("iry_worker_id"));
			$status = $worker->updateItem("iry_worker_info", $set_, $where_);
		
			if($status) session("iry_worker_id",null);
			return $status?"success":"false";
		}else{
			$insert_ = array(
					"name"	=>	"'$name'",
					"sex"	=>	$sex,
					"birthday"	=>	"'$birthday'",
					"email"		=>	"'$email'",
					"branch"	=>	"'$branch'",
					"phonenum"	=>	"'$phonenum'",
					"banknum"	=>	"'$banknum'",
					"bank"		=>	"'$bank'",
					"entryday"	=>	"'$entryday'",
					"leaveday"	=>	"'$leaveday'",
					"persionid"	=>	"'$persionid'",
					"workerip"	=>	"'$workerip'"
			);
			$status= $worker->insertItem("iry_worker_info", $insert_);
			return $status?"success":"false";
		}
	}
	
	//显示个人信息
	function viewInfo(){
		$name = session("iry_worker_name");
		$sex = session("iry_worker_sex");
		$birthday = session("iry_worker_birthday");
		$faceimg = session("iry_worker_faceimg");
		$picture = session("iry_worker_picture");
		$phonenum = session("iry_worker_phonenum");
		$email = session("iry_worker_email");
		$branch = session("iry_worker_branch");
		$banknum = session("iry_worker_banknum");
		$bank = session("iry_worker_bank");
		$persionid = session("iry_worker_persionid");
		$entryday = session("iry_worker_entryday");
		$leaveday = session("iry_worker_leaveday");
		
		if($faceimg == null) $faceimg = "Public/Image/faceimg.jpg";
		$faceimg = C('web_root').$faceimg;
		
		$sex = C("sex_name.".$sex);
		$branch = C("branch_name.".$branch);
		return "<table cellpadding='0' cellspacing='0' id='table_worker_info'>
				<tr><td width='100'>姓名:</td><td width='300'>$name</td>
					<td rowspan='12' id='td_faceimg' align='center' valign='top' width='500'>
						<img src='$faceimg' id='worker_faceimg' onclick='enFaceimgPanel()' title='点击更换头像'></td>
					<td rowspan='12' id='td_blank'></td></tr>
				<tr><td>性别:</td><td>$sex</td></tr>
				<tr><td>生日:</td><td>$birthday</td></tr>
				<tr><td>邮箱:</td><td>$email</td></tr>
				<tr><td>部门:</td><td>$branch</td></tr>
				<tr><td>手机号:</td><td>$phonenum</td></tr>
				<tr><td>银行账号:</td><td>$banknum</td></tr>
				<tr><td>开户银行:</td><td>$bank</td></tr>
				<tr><td>入职日期:</td><td>$entryday</td></tr>
				<tr><td>离职日期:</td><td>$leaveday</td></tr>
				<tr><td>身份证号:</td><td>$persionid</td></tr>
				<tr><td></td><td><input type='button' onclick='editWorkerInfo()' value='编辑信息' class='button-3'></td></tr>
				</table>";
	}
	//附件上传(非ajax)
	function upFile(){
		$file_type = $_REQUEST['file-type'];
		$file = $_FILES['worker-file'];
		
		if($file[tmp_name] != ""){
			$upload	=	new \Think\Upload();
			$upload->rootPath	=	'./Public/File/';
			$upload->savePath	=	'';
				
			$info	=	$upload->uploadOne($file);
			if(!$info){
				$this->error($upload->getError());
			}else{
				$filesrc	=	"Public/File/".date("Y-m-d")."/".$info['savename'];
			}
			
			$worker = new \Home\Common\Worker();
			$workerid = session("iry_worker_id");
			$status = $worker->insertItem("iry_worker_file", array("workerid"=>$workerid,"type"=>"'$file_type'","filesrc"=>"'$filesrc'"));
			
			if($status){
				$this->redirect("/Home/Index/ajaxWorker");
			}else{
				$this->error("文件信息写入失败!请重试!");
			}
		}else{
			$this->error("文件信息获取失败!请重试!");
		}
		
	}
	//头像上传
	function upFaceImg(){
		$faceimg = $_FILES['worker-faceimg'];
		
		if($faceimg[tmp_name] != ""){
			$upload	=	new \Think\Upload();
			$upload->rootPath	=	'./Public/Image/';
			$upload->savePath	=	'';
		
			$info	=	$upload->uploadOne($faceimg);
			if(!$info){
				$this->error($upload->getError());
			}else{
				$filesrc	=	"Public/Image/".date("Y-m-d")."/".$info['savename'];
			}
				
			$worker = new \Home\Common\Worker();
			$workerid = session("iry_worker_id");
			$status = $worker->updateItem("iry_worker_info", array("faceimg"=>"'$filesrc'"), array("workerid"=>$workerid));
				
			if($status){
				$this->redirect("/Home/Index/ajaxWorker");
			}else{
				$this->error("文件信息写入失败!请重试!");
			}
		}else{
			$this->error("文件信息获取失败!请重试!");
		}
	}
	
}